<?php

defined('_JEXEC') or die('Restricted access');

function com_install() {
    global $mainframe;
	$db =& JFactory::getDBO();
	
	
	$query = "SELECT count(1) FROM #__modules"
	." WHERE module = 'mod_jckquickicon'";
	$db->setQuery( $query );
	$count = $db->loadResult();
	if($count)  com_uninstall();
	
	
	jimport('joomla.filesystem.folder');
		
	$src 	= 'components' .DS. 'com_jckman' .DS. 'modules' .DS. 'mod_jckquickicon';
	$dest 	= 'modules' .DS. 'mod_jckquickicon';
	
	if( !JFolder::copy( $src, $dest, JPATH_ADMINISTRATOR, true ) ){
		$mainframe->enqueueMessage( JText::_('Unable to install control panel icon module!') );
	}

	$row = & JTable::getInstance('module');
	$row->title = 'Welcome';
	$row->content = 	
	'<div style="padding: 5px;">
		Congratulations on installing the official component for the JoomlaCK editor.</div>
	<div style="padding: 5px;">
		<img alt="" src="components/com_jckman/icons/icon-24-man.png" style="float: left; margin: 3px 2px 0pt 0pt;" />The JoomlaCK Editor Manager opens up a world of possibilities! In it simplest form - it&#39;s a way of effortlessly extending the JoomlaCK framework without the need of programming knowledge! And because the JoomlaCK editor is built upon a state-of-the-art plug-and-play framework you can expand it and shape it in any direction!<br />
		<br />
	</div>';
	$row->ordering = $row->getNextOrder( "position='jck_cpanel'" );
	$row->position = 'jck_cpanel';
	$row->published = 1;
	$row->showtitle = 1;
	$row->iscore = 0;
	$row->access = 0;
	$row->client_id = 1;
	$row->module = 'mod_custom';
	$row->params = '';
	if (!$row->store()) {
		$mainframe->enqueueMessage( JText::_('Unable to insert JCK PlugMan custom Module data!') );
	}	
	
	$row->id = 0;
    $row->title = 'Installer';
	$row->content = 	
	'<div style="padding: 5px;">
		<img alt="" src="components/com_jckman/icons/icon-24-installer.png" style="float: left; margin: 3px 2px 0pt 0pt;" />The installer allows the user to install/uninstall 3rd party add-ons into the editor. This works for Plugins but will extend to include Languages and Skins in the near future. We plan to take it even further to unleash the full power of the JCK framework. This will permit developers to simply modify the editor&#39;s actual framework with PHP plugins!
		<br />
		<br />
	</div>';
	$row->ordering = $row->getNextOrder( "position='jck_cpanel'" );
	if (!$row->store()) {
		$mainframe->enqueueMessage( JText::_('Unable to insert JCK PlugMan custom Module data!') );
	}
	
	$row->id = 0;
    $row->title = 'JCK Plugins';
	$row->content = 	
	'<div style="padding: 5px;">
		<img alt="" src="components/com_jckman/icons/icon-24-plugin.png" style="float: left; margin: 3px 2px 0pt 0pt;" />A JCK plugin consists of a program that interacts with the editor. These plugins (or apps) are used to enable the user to quickly and simply customize or expand the functionality of the editor. This could be done to meet your needs to portfolio images in a stylish gallery light-box or used to add document management capabilities; the possibilities are endless!
		<br />
		<br />
	</div>';
	$row->ordering = $row->getNextOrder( "position='jck_cpanel'" );
	$row->params = '';
	if (!$row->store()) {
		$mainframe->enqueueMessage( JText::_('Unable to insert JCK PlugMan custom Module data!') );
	}
	
	$row->id = 0;
	$row->title = 'System Check';
	$row->content = 	
	'<div style="padding: 5px;">
		<img alt="" src="components/com_jckman/icons/icon-24-systemcheck.png" style="float: left; margin: 3px 2px 0pt 0pt;" />If you have experience installation or usage issues with the JCK editor, this is for YOU! The component now has an intelligent API that will work to rectify any server-side file permission issue within itself. And if it is unable to do this, it will automatically perform a health check and notify you of the issue.
	</div>';
	$row->ordering = $row->getNextOrder( "position='jck_cpanel'" );
	if (!$row->store()) {
		$mainframe->enqueueMessage( JText::_('Unable to insert JCK PlugMan custom Module data!') );
	}
	
	$row->id = 0;
	$row->title = 'Support';
	$row->content = 
	'<div style="padding: 5px;">
		<img alt="" src="components/com_jckman/icons/icon-24-support.png" style="float: left; margin: 3px 2px 0pt 0pt;" />If you have a question please stop by, we have a collection of <a href="http://www.joomlackeditor.com/tutorials" target="_blank">comprehensive tutorials</a>, a <a href="http://www.joomlackeditor.com/forum" target="_blank">community area</a> for people to exchange ideas and help each other out.  For those who are using the editor professionally we have a high priority support and ticketing system, available on <a href="http://www.joomlackeditor.com/subscription" target="_blank">subscription</a>.
		<br />
		<br />
	</div>';
	$row->ordering = $row->getNextOrder( "position='jck_cpanel'" );
	if (!$row->store()) {
		$mainframe->enqueueMessage( JText::_('Unable to insert JCK PlugMan custom Module data!') );
	}
	
	

	$row = & JTable::getInstance('module');
	$row->title = 'JCK PLugMan Control Panel Icons';
	$row->ordering = $row->getNextOrder( "position='jck_icon'" );
	$row->position = 'jck_icon';
	$row->published = 1;
	$row->showtitle = 0;
	$row->iscore = 0;
	$row->access = 0;
	$row->client_id = 1;
	$row->module = 'mod_jckquickicon';
	$row->params = '';

	if (!$row->store()) {
		$mainframe->enqueueMessage( JText::_('Unable to insert Control Panel icon Module data!') );
	}
		
	
	
	jimport('joomla.filesystem.file');
		
	$src 	= JPATH_ADMINISTRATOR.DS.'components' .DS. 'com_jckman' .DS. 'editor' .DS. 'pluginoverrides.php';
	$dest 	= JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'includes'.DS.'ckeditor'.DS.'plugins'.DS.'editor'.DS. 'pluginoverrides.php';
	
	
	if( !JFile::copy( $src, $dest) ){
		$mainframe->enqueueMessage( JText::_('Unable to move pluginoverrides JCK plugin!') );
	}
	
		
	$src 	= JPATH_ADMINISTRATOR.DS .'components' .DS. 'com_jckman' .DS. 'editor'.DS.'plugins.php';
	$dest 	= JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'includes'.DS.'ckeditor'.DS.'plugins.php';
	
	
	if( !JFile::copy( $src, $dest) ){
		$mainframe->enqueueMessage( JText::_('Unable to move base plugins file to JCK library!') );
	}
	
	
	$src 	= JPATH_ADMINISTRATOR.DS .'components' .DS. 'com_jckman' .DS. 'editor'.DS.'includes.php';
	$dest 	= JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'includes.php';
	
	
	if( !JFile::copy( $src, $dest) ){
		$mainframe->enqueueMessage( JText::_('Unable to move updated includes file to JCK plugin!') );
	}
		
	$src 	= JPATH_ADMINISTRATOR.DS.'components' .DS. 'com_jckman' .DS. 'editor' .DS. 'scayt.xml';
	$dest 	= JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'plugins'.DS.'scayt'.DS. 'scayt.xml';
	
	
	if( !JFile::copy( $src, $dest) ){
		$mainframe->enqueueMessage( JText::_('Unable to move scayt JCK plugin!') );
	}
		
	unset( $row );
	
	//Check System requirements for the editor 
	
	define('JCK_BASE',JPATH_CONFIGURATION .DS.'plugins'.DS.'editors'.DS.'jckeditor');
		
	if(!JFolder::exists(JCK_BASE))
	{
		$mainframe->enqueueMessage( JText::_('System has detected JCK Editor has not been installed!') );
	 	return;
	}
	
	$perms  = fileperms(JPATH_CONFIGURATION.DS.'index.php');
	$perms = (decoct($perms & 0777));
		
	$default_fperms = '0644';
	$default_dperms = '0755'; 
		
	if($perms == 777 || $perms == 666)
	{
		$default_fperms = '0666';
		$default_dperms = '0777'; 
	}
	
	$fperms = JCK_BASE.DS.'config.js';
	
	if(!stristr(PHP_OS,'WIN') && JPath::canChmod(JCK_BASE)  && $perms != decoct(fileperms($fperms) & 0777))
	{
		
	  $path = JCK_BASE.DS.'plugins';
	 
	  if(!JPath::setPermissions($path,$default_fperms,$default_dperms))
	  {
	   	$mainframe->enqueueMessage( JText::_( 'System detected incorrect file permissons for the JCK Editor. Unable to autocorrect so please mannually change.') );
	  }
	}
}
function com_uninstall() {
	global $mainframe;
	$db =& JFactory::getDBO();
	jimport('joomla.filesystem.folder');
	
	$path = JPATH_ADMINISTRATOR .DS. 'modules' .DS. 'mod_jckquickicon';
	
	if( !JFolder::delete( $path ) ){
		$mainframe->enqueueMessage( JText::_('Unable to remove control panel icon module!') );
	}
	
	$query = "DELETE FROM #__modules"
	." WHERE module = 'mod_custom'"
	." AND position = 'jck_cpanel'";
	$db->setQuery( $query );
	
	if( !$db->query() ){
		$mainframe->enqueueMessage( JText::_('Unable to remove JCK PlugMan custom Module data!') );
	}
	
	$query = "DELETE FROM #__modules"
	." WHERE module = 'mod_jckquickicon'";
	$db->setQuery( $query );
	
	if( !$db->query() ){
		$mainframe->enqueueMessage( JText::_('Unable to remove Control Panel icon Module data!') );
	}
	
	$file = JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'includes'.DS.'ckeditor'.DS.'plugins'.DS.'editor'.DS. 'pluginoverrides.php';
	
	if(JFile::exists($file) && !JFile::delete($file)) {
		$mainframe->enqueueMessage( JText::_('Unable to delete pluginoverrides JCK plugin!') );
	}
	
	$file = JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'plugins'.DS.'scayt'.DS.'scayt.xml';
	
	if(JFile::exists($file) && !JFile::delete($file)) {
		$mainframe->enqueueMessage( JText::_('Unable to delete scayt JCK plugin install file!') );
	}

	
}